Option Strict On

Imports System.Web
Imports System.Xml
Imports System.Collections.Generic

Imports System.Security.Cryptography
Imports System.Web.Configuration

Imports DnnPerm = DotNetNuke.Security.Permissions
Imports DnnRoles = DotNetNuke.Security.Roles
Imports DnnFS = DotNetNuke.Services.FileSystem

Imports System.ServiceModel
Imports System.Runtime.Serialization
Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Services.Scheduling
Imports DotNetNuke.Services.FileSystem




Namespace DotNetNuke.Modules.IWebCF

    Partial Public Class CoreService
        Implements IWebFileManager


#Region "Consts"

        Private Const Pattern_TotalSpaceUsed As String = "Using: {0}"
        Private Const Pattern_TotalSpaceUsedWithQuota As String = "Used: {0} of {1}"

        Private Const Setting_MaxRetries As Integer = 3000
        Private Const Setting_MaxSeconds As Integer = 10
        Private Const Permission_ReadId As Integer = 5
        Private Const Permission_WriteId As Integer = 6


        'Private Const RegEx_FolderNameWithParents As String = "^[a-zA-Z0-9\-\s_\/]{1,250}$"
        'Private Const RegEx_FolderNameWithOutParents As String = "^[a-zA-Z0-9\-\s_]{1,250}$"
        'Private Const RegEx_FileNameWithFolders As String = "^[a-zA-Z0-9\-\s_./]{1,250}$"
        'Private Const RegEx_FileNameWithOutFolders As String = "^[a-zA-Z0-9\-\s_.]{1,250}$"


#End Region

#Region "Private Methods"
        Private Function CleanErrorMessage(ByVal portalId As Integer, ByVal strErrorMessage As String) As String
            Dim cleanedError As String = "There has been an error."


            If HttpContext.Current IsNot Nothing Then

                cleanedError = strErrorMessage.Replace _
                    (HttpContext.Current.Request.MapPath("~/Portals/" & portalId.ToString), String.Empty)

            End If

            Return cleanedError

        End Function

        Private Shared Function ConvertToGenericListOfFolderInfo(ByVal folderArray As ArrayList) As List(Of FolderInfoDataContract)

            Dim folderList As New List(Of FolderInfoDataContract)

            Try


                For Each folder As DnnFS.FolderInfo In folderArray

                    folderList.Add(New FolderInfoDataContract(folder.FolderID, folder.FolderName))

                Next

            Catch castEx As InvalidCastException

                Return Nothing

            Catch ex As Exception

                Return Nothing

            End Try

            Return folderList

        End Function

        Private Shared Function ConvertToGenericListOfFileInfo(ByVal fileArray As ArrayList) As List(Of FileInfoDataContract)

            Dim fileList As New List(Of FileInfoDataContract)

            Try


                For Each file As DnnFS.FileInfo In fileArray

                    fileList.Add(New FileInfoDataContract(file))

                Next

            Catch castEx As InvalidCastException

                Return Nothing

            Catch ex As Exception

                Return Nothing

            End Try

            Return fileList

        End Function


        Private Shared Function ConvertToMB(ByVal bytes As Long) As Long

            Return CLng((bytes / (1024 * 1024)))

        End Function




        Private Function GetRelativeFilePath(ByVal portalId As Integer, ByVal fileId As Integer) As String

            Dim fileController As New DnnFS.FileController

            Dim file As DnnFS.FileInfo = fileController.GetFileById(fileId, portalId)

            Return IO.Path.Combine(file.Folder, file.FileName)

        End Function

        Private Function GetRelativeFilePath(ByVal portalId As Integer, ByVal folderId As Integer, ByVal fileName As String) As String

            Dim cleanFileName As String = IO.Path.GetFileName(fileName)

            Return IO.Path.Combine(Me.GetRelativeFolderPath(portalId, folderId), cleanFileName)

        End Function

        Private Function GetFileMapPath(ByVal header As IWebAuthenticationHeader, ByVal fileId As Integer) As String

            Dim info As FileInfoDataContract = Me.GetFileById(New FileIdRequest(header, fileId)).Value

            Return Me.GetFileMapPath(header, info.FolderId, info.FileName)

        End Function

        Private Function GetFileMapPath(ByVal header As IWebAuthenticationHeader, ByVal folderId As Integer, ByVal fileName As String) As String

            Dim settings As PortalInfo

            Dim pc As New PortalController

            settings = pc.GetPortal(header.PortalID)

            Dim filePath As String = Me.GetRelativeFilePath(header.PortalID, folderId, fileName)


            Return IO.Path.Combine(settings.HomeDirectoryMapPath, filePath)

            'Return My.Request.MapPath("~/" & settings.HomeDirectory & "/" & filePath)

        End Function

        Private Function GetHomeDirectoryFolderId(ByVal portalId As Integer) As Integer

            Dim folderController As New DnnFS.FolderController()

            Dim folder As DnnFS.FolderInfo = FileSystemUtils.GetFolder _
                (portalId, String.Empty)

            If folder IsNot Nothing Then

                Return folder.FolderID

            Else

                Return -1

            End If


        End Function

        Private Function GetFolderMapPath(ByVal portalID As Integer, ByVal folderId As Integer) As String

            Dim relativePath As String = Me.GetRelativeFolderPath(portalID, folderId)

            If Not String.IsNullOrEmpty(relativePath) Then

                Return IO.Path.Combine(GetPortalInfo(portalID).HomeDirectoryMapPath, relativePath)


            Else

                Return GetPortalInfo(portalID).HomeDirectoryMapPath

            End If

        End Function

        Private Function GetRelativeFolderPath(ByVal portalId As Integer, ByVal folderId As Integer) As String

            Dim folderControl As New DnnFS.FolderController

            If folderId < 1 Then

                Return String.Empty

            End If

            Dim folder As DotNetNuke.Services.FileSystem.FolderInfo

            folder = folderControl.GetFolderInfo(portalId, folderId)

            If folder IsNot Nothing Then

                Return folder.FolderPath

            Else

                Return String.Empty

            End If


        End Function

        Private Function GetLoggedNotAuthorizedEx(ByVal header As IWebAuthenticationHeader) As FaultException(Of String)

            Dim dnnEx As New FaultException(Of String) _
                ("Not Authorized to preform this function: " & HttpContext.Current.Request.RawUrl & _
                 " Username: " & header.Username)


            Exceptions.LogException(dnnEx)


            Dim soapEx As New FaultException(Of String) _
                ("Not Authorized to preform this function. Username: " & _
               header.Username)


            Return soapEx

        End Function


        Private Function HasFileAccess(ByVal header As IWebAuthenticationHeader, ByVal fileId As Integer, ByVal permissionType As Integer) As Boolean


            Dim fileController As New DnnFS.FileController

            Dim file As DnnFS.FileInfo = fileController.GetFileById(fileId, header.PortalID)

            Return Me.HasFolderAccess(header, file.FolderId, permissionType)

        End Function

        Private Function HasFolderAccess(ByVal header As IWebAuthenticationHeader, ByVal folderId As Integer, ByVal permissionType As Integer) As Boolean


            Dim user As UserInfo = UserController.GetUserByName _
                (header.PortalID, header.Username, False)

            header.UserID = user.UserID

            If user.IsSuperUser Then Return True

            Dim roleCtrl As New DnnRoles.RoleController

            user.Roles = roleCtrl.GetRolesByUser(user.UserID, header.PortalID)

            Dim ctrl As New DnnPerm.FolderPermissionController

            Dim folderPermsList As DnnPerm.FolderPermissionCollection

            folderPermsList = ctrl.GetFolderPermissionsCollectionByFolderPath _
                (header.PortalID, Me.GetRelativeFolderPath(header.PortalID, folderId))


            For Each perm As DnnPerm.FolderPermissionInfo In folderPermsList


                If perm.AllowAccess AndAlso perm.PermissionID = permissionType _
                    AndAlso (perm.UserID = header.UserID _
                    OrElse user.IsInRole(perm.RoleName)) Then

                    Return True

                ElseIf Not perm.AllowAccess AndAlso perm.PermissionID = permissionType _
                    AndAlso (perm.UserID = header.UserID _
                    OrElse user.IsInRole(perm.RoleName)) Then

                    Return False


                End If

            Next

            Return False

        End Function


        Private Shared Function CheckFileType(ByVal ext As String) As Boolean

            Dim exts As New List(Of String) _
                    (HostSettings("FileExtensions").ToString.Split(","c))

            Return exts.Contains(ext.Replace(".", "").ToLowerInvariant)

        End Function

        Private Function GetPortalInfo(ByVal portalId As Integer) As PortalInfo
            Dim ctrl As New PortalController

            Return ctrl.GetPortal(portalId)

        End Function


        Private Sub SyncFilesSystem(ByVal portalId As Integer)

            Dim fc As New DnnFS.SynchronizeFileSystem(New ScheduleHistoryItem())
            fc.DoWork()


            FileSystemUtils.SynchronizeFolder(portalId, _
                    GetPortalInfo(portalId).HomeDirectoryMapPath, String.Empty, True)

        End Sub


#End Region

#Region "IWebFileManager"


        <IWebMethod( _
            Company:="DotNetNuke", _
            Application:="IWeb", _
            Name:="AddFolder", _
            Description:="Add a new folder to specified parent or portal root.", _
            Group1:="IWeb FileManager", Group2:="Portal", _
            DefaultSecuritySetting:=IWebMethodAttribute.MethodSecurityLevels.Host)> _
        Public Sub AddFolder(ByVal msg As AddFolderRequest) Implements IWebFileManager.AddFolder

            Dim authendication As New IWebAuthentication(msg.Header)

            If authendication.ValidAndAuthorized() AndAlso _
                Me.HasFolderAccess(msg.Header, msg.ParentFolderId, Permission_WriteId) Then




                Try



                    'Dim parentFolder As String = Me.GetFolderMapPath _
                    '                            (msg.Header.PortalID, msg.ParentFolderId)
                    'Dim settings As PortalSettings = New PortalController().GetPortal(msg.Header.PortalID)
                    'FileSystemUtils.AddFolder(settings, parentFolder, msg.NewFolderName)


                    Dim folderCtrl As New DnnFS.FolderController()


                    If msg.ParentFolderId > -1 Then

                        Dim parent As FolderInfo = folderCtrl.GetFolderInfo(msg.Header.PortalID, msg.ParentFolderId)

                        IO.Directory.CreateDirectory(parent.PhysicalPath)

                        IO.Directory.CreateDirectory(IO.Path.Combine(parent.PhysicalPath, msg.NewFolderName))

                        folderCtrl.AddFolder(msg.Header.PortalID, IO.Path.Combine(parent.FolderPath, msg.NewFolderName))

                    Else


                        Dim dir As String = GetPortalInfo(msg.Header.PortalID).HomeDirectoryMapPath

                        IO.Directory.CreateDirectory(dir)

                        IO.Directory.CreateDirectory(IO.Path.Combine(dir, msg.NewFolderName))

                        folderCtrl.AddFolder(msg.Header.PortalID, msg.NewFolderName)


                    End If

                Catch ex As Exception

                    Throw New FaultException(Of String) _
                        (ex.Message, CleanErrorMessage(msg.Header.PortalID, ex.Message))

                End Try

            Else


                Throw Me.GetLoggedNotAuthorizedEx(msg.Header)

            End If
        End Sub

        <IWebMethod( _
            Company:="DotNetNuke", _
            Application:="IWeb", _
            Name:="CopyFile", _
            Description:="Copies a file to a new file with the specified name.", _
            Group1:="IWeb FileManager", Group2:="Portal", _
            DefaultSecuritySetting:=IWebMethodAttribute.MethodSecurityLevels.Host)> _
        Public Function CopyFile(ByVal msg As CopyFileRequest) As IWebResponse(Of String) Implements IWebFileManager.CopyFile
            Dim authendication As New IWebAuthentication(msg.Header)

            If authendication.ValidAndAuthorized() _
                AndAlso Me.HasFileAccess(msg.Header, msg.SourceFileID, Permission_ReadId) _
                AndAlso Me.HasFolderAccess(msg.Header, msg.TargetFolderId, Permission_WriteId) Then

                Dim sourceFile As String = Me.GetRelativeFilePath(msg.Header.PortalID, msg.SourceFileID)

                msg.TargetFileName = Me.GetRelativeFilePath(msg.Header.PortalID, msg.TargetFolderId, msg.TargetFileName)

                Try

                    Return New IWebResponse(Of String) _
                        (FileSystemUtils.CopyFile(sourceFile, msg.TargetFileName, _
                        PortalController.GetCurrentPortalSettings))


                Catch ex As Exception

                    Throw New FaultException(Of String)(CleanErrorMessage(msg.Header.PortalID, ex.Message))


                End Try


            Else
                Throw Me.GetLoggedNotAuthorizedEx(msg.Header)


            End If

        End Function


        <IWebMethod( _
            Company:="DotNetNuke", _
            Application:="IWeb", _
            Name:="DefaultProtectedFolders", _
            Description:="Determines if specified path is the default protected folder. ", _
            Group1:="IWeb FileManager", Group2:="Portal", _
            DefaultSecuritySetting:=IWebMethodAttribute.MethodSecurityLevels.Host)> _
        Public Function DefaultProtectedFolders(ByVal msg As FolderIdRequest) As IWebResponse(Of Boolean) Implements IWebFileManager.DefaultProtectedFolders


            Dim authendication As New IWebAuthentication(msg.Header)

            If authendication.ValidAndAuthorized() _
                AndAlso Me.HasFolderAccess(msg.Header, msg.FolderID, Permission_ReadId) Then


                Try

                    Dim folderPath As String = Me.GetRelativeFolderPath(msg.Header.PortalID, msg.FolderID)


                    Return New IWebResponse(Of Boolean)(FileSystemUtils.DefaultProtectedFolders(folderPath))

                Catch ex As Exception

                    Throw New FaultException(Of String)(CleanErrorMessage(msg.Header.PortalID, ex.Message))

                End Try

            Else


                Throw Me.GetLoggedNotAuthorizedEx(msg.Header)

            End If



        End Function

        '<IWebMethod( _
        '    Company:="DotNetNuke", _
        '    Application:="IWeb", _
        '    Name:="DeleteFiles", _
        '    Description:="Delete the list of specified files.", _
        '    Group1:="IWeb FileManager", Group2:="Portal", _
        '    DefaultSecuritySetting:=IWebMethodAttribute.MethodSecurityLevels.Host)> _
        'Public Function DeleteFiles(ByVal msg As DeleteFilesRequest) As IWebResponse(Of String) Implements IWebFileManager.DeleteFiles

        '    Dim authendication As New IWebAuthentication(msg.Header)

        '    If Not authendication.ValidAndAuthorized() Then

        '        Throw Me.GetLoggedNotAuthorizedEx(msg.Header)

        '    End If


        '    Dim strErrorMessage As String = String.Empty

        '    Try

        '        Dim i As Integer
        '        If msg.FileIds.Count = 0 Then

        '            Return New IWebResponse(Of String)("0")

        '            Exit Function
        '        End If

        '        Dim strCurError As String = String.Empty

        '        For i = 0 To msg.FileIds.Count - 1

        '            Dim timedOut As Boolean = False

        '            If Me.HasFileAccess(msg.Header, msg.FileIds.Item(i), Permission_WriteId) Then

        '                Dim deleted As Boolean = False

        '                Dim startTime As DateTime = Now


        '                Do Until deleted OrElse timedOut


        '                    Try

        '                        Dim fileCtrl As New FileController

        '                        Dim file As FileInfo = fileCtrl.GetFileById(msg.Header.PortalID, msg.FileIds(i))

        '                        fileCtrl.DeleteFile(msg.Header.PortalID, file.FileName, file.FolderId, True)

        '                        'FileSystemUtils.DeleteFile(Me.GetRelativeFilePath _
        '                        '        (msg.Header.PortalID, msg.FileIds.Item(i)), _
        '                        '        PortalController.GetCurrentPortalSettings)

        '                        If IO.File.Exists(file.PhysicalPath) Then

        '                            'IO.File.Delete(Me.GetFileMapPath(msg.Header, msg.FileIds.Item(i)))
        '                            IO.File.Delete(file.PhysicalPath)

        '                        End If

        '                        deleted = True

        '                    Catch authEx As UnauthorizedAccessException

        '                        Throw New FaultException("Access denied.")

        '                    Catch ex As Exception

        '                        deleted = False

        '                        timedOut = DateAdd(DateInterval.Second, Setting_MaxSeconds, startTime) < Now

        '                        If timedOut Then

        '                            strCurError &= ex.Message & vbCrLf

        '                            Exit Do

        '                        End If

        '                    End Try

        '                Loop

        '            Else

        '                strCurError = "Access denied. Please check file permissions and try again."

        '            End If

        '            If Not String.IsNullOrEmpty(strCurError) OrElse timedOut Then

        '                strErrorMessage &= " Error Deleting File: " & strCurError

        '            End If

        '        Next


        '        Me.SyncFilesSystem(msg.Header.PortalID)


        '    Catch ex As Exception

        '        Throw New FaultException(Of String)(ex.Message, CleanErrorMessage(msg.Header.PortalID, ex.Message))

        '    End Try

        '    If Not String.IsNullOrEmpty(strErrorMessage) Then


        '        Throw New FaultException(CleanErrorMessage(msg.Header.PortalID, strErrorMessage))

        '    End If

        '    Return New IWebResponse(Of String)(msg.FileIds.Count.ToString)


        'End Function


        <IWebMethod( _
            Company:="DotNetNuke", _
            Application:="IWeb", _
            Name:="GetFileById", _
            Description:="Get a FileInfo object for the specified FileId. ", _
            Group1:="IWeb FileManager", Group2:="Portal", _
            DefaultSecuritySetting:=IWebMethodAttribute.MethodSecurityLevels.Host)> _
        Public Function GetFileById(ByVal msg As FileIdRequest) As IWebResponse(Of FileInfoDataContract) Implements IWebFileManager.GetFileById


            Dim authendication As New IWebAuthentication(msg.Header)

            If authendication.ValidAndAuthorized() _
                AndAlso Me.HasFileAccess(msg.Header, msg.FileId, Permission_ReadId) Then


                Dim fileController As New DnnFS.FileController

                Dim file As DnnFS.FileInfo = fileController.GetFileById(msg.FileId, msg.Header.PortalID)

                Return New IWebResponse(Of FileInfoDataContract)(New FileInfoDataContract(file))

            Else


                Throw Me.GetLoggedNotAuthorizedEx(msg.Header)


            End If


        End Function


        <IWebMethod( _
            Company:="DotNetNuke", _
            Application:="IWeb", _
            Name:="GetFilesByFolder", _
            Description:="Gets list of file by in the specified folder.", _
            Group1:="IWeb FileManager", Group2:="Portal", _
            DefaultSecuritySetting:=IWebMethodAttribute.MethodSecurityLevels.Host)> _
        Public Function GetFilesByFolder(ByVal msg As FolderIdRequest) As IWebResponse(Of System.Collections.Generic.List(Of FileInfoDataContract)) Implements IWebFileManager.GetFilesByFolder


            Dim authendication As New IWebAuthentication(msg.Header)

            If authendication.ValidAndAuthorized() _
                AndAlso Me.HasFolderAccess(msg.Header, msg.FolderID, Permission_ReadId) Then


                Try


                    If msg.FolderID < 1 Then msg.FolderID = Me.GetHomeDirectoryFolderId(msg.Header.PortalID)


                    Return New IWebResponse(Of List(Of FileInfoDataContract))(ConvertToGenericListOfFileInfo _
                        (FileSystemUtils.GetFilesByFolder(msg.Header.PortalID, msg.FolderID)))


                Catch ex As Exception

                    LogException(ex)

                    Throw New FaultException(Of String)(CleanErrorMessage(msg.Header.PortalID, ex.Message))

                End Try

            Else

                Throw Me.GetLoggedNotAuthorizedEx(msg.Header)


            End If

        End Function

        <IWebMethod( _
            Company:="DotNetNuke", _
            Application:="IWeb", _
            Name:="GetFolderById", _
            Description:="Get a FolderInfo object for the specified FolderId.", _
            Group1:="IWeb FileManager", Group2:="Portal", _
            DefaultSecuritySetting:=IWebMethodAttribute.MethodSecurityLevels.Host)> _
        Public Function GetFolderById(ByVal msg As FolderIdRequest) As IWebResponse(Of FolderInfoDataContract) Implements IWebFileManager.GetFolderById

            Dim authendication As New IWebAuthentication(msg.Header)

            If authendication.ValidAndAuthorized() _
                AndAlso Me.HasFolderAccess(msg.Header, msg.FolderID, Permission_ReadId) Then


                Dim folderController As New DnnFS.FolderController

                Dim folder As DnnFS.FolderInfo = folderController _
                    .GetFolderInfo(msg.Header.PortalID, msg.FolderID)

                Return New IWebResponse(Of FolderInfoDataContract)(New FolderInfoDataContract(folder))


            Else

                Throw Me.GetLoggedNotAuthorizedEx(msg.Header)

            End If

        End Function

        <IWebMethod( _
             Company:="DotNetNuke", _
             Application:="IWeb", _
             Name:="GetFoldersByParent", _
             Description:="Gets list of folders by parent.", _
             Group1:="IWeb FileManager", Group2:="Portal", _
             DefaultSecuritySetting:=IWebMethodAttribute.MethodSecurityLevels.Host)> _
        Public Function GetFoldersByParent(ByVal msg As FolderIdRequest) As IWebResponse(Of System.Collections.Generic.List(Of FolderInfoDataContract)) Implements IWebFileManager.GetFoldersByParent

            Dim authendication As New IWebAuthentication(msg.Header)

            If authendication.ValidAndAuthorized() _
                AndAlso Me.HasFolderAccess(msg.Header, msg.FolderID, Permission_ReadId) Then



                Try

                    Dim parentFolder As String = Me.GetRelativeFolderPath(msg.Header.PortalID, msg.FolderID)

                    Return New IWebResponse(Of List(Of FolderInfoDataContract)) _
                        (ConvertToGenericListOfFolderInfo _
                        (FileSystemUtils.GetFoldersByParentFolder _
                        (msg.Header.PortalID, parentFolder)))


                Catch ex As Exception
                    LogException(ex)

                    Throw New FaultException(Of String)(CleanErrorMessage(msg.Header.PortalID, ex.Message))

                End Try

            Else

                Throw Me.GetLoggedNotAuthorizedEx(msg.Header)


            End If
        End Function

        <IWebMethod( _
            Company:="DotNetNuke", _
            Application:="IWeb", _
            Name:="GetMaxRequestLength", _
            Description:="Gets maximum request size.", _
            Group1:="IWeb FileManager", Group2:="Portal", _
            DefaultSecuritySetting:=IWebMethodAttribute.MethodSecurityLevels.Host)> _
        Public Function GetMaxRequestLength(ByVal msg As IWebRequest) As IWebResponse(Of Long) Implements IWebFileManager.GetMaxRequestLength
            Dim authentication As New IWebAuthentication(msg.Header)

            If authentication.ValidAndAuthorized _
                AndAlso Me.HasFolderAccess(msg.Header, Me.GetHomeDirectoryFolderId _
                        (msg.Header.PortalID), Permission_ReadId) Then

                Try

                    Dim result As Long = CLng(IWebConfigController.GetMaxRequestLength)

                    Return New IWebResponse(Of Long)(result)


                Catch ex As Exception

                    LogException(ex)

                    Return New IWebResponse(Of Long)(-1)

                End Try

            Else

                Throw Me.GetLoggedNotAuthorizedEx(msg.Header)

            End If
        End Function

        <IWebMethod( _
           Company:="DotNetNuke", _
           Application:="IWeb", _
           Name:="GetPortalRootFolders", _
           Description:="Gets folders in the Portal Home Directory.", _
           Group1:="IWeb FileManager", Group2:="Portal", _
           DefaultSecuritySetting:=IWebMethodAttribute.MethodSecurityLevels.Host)> _
        Public Function GetPortalRootFolders(ByVal msg As IWebRequest) As IWebResponse(Of System.Collections.Generic.List(Of FolderInfoDataContract)) Implements IWebFileManager.GetPortalRootFolders

            Dim authendication As New IWebAuthentication(msg.Header)

            If authendication.ValidAndAuthorized() _
                AndAlso Me.HasFolderAccess(msg.Header, Me.GetHomeDirectoryFolderId(msg.Header.PortalID), Permission_ReadId) Then

                Try

                    Return New IWebResponse(Of List(Of FolderInfoDataContract))(ConvertToGenericListOfFolderInfo _
                        (FileSystemUtils.GetFolders(msg.Header.PortalID)))

                Catch ex As Exception
                    LogException(ex)

                    Throw New FaultException(Of String)(CleanErrorMessage(msg.Header.PortalID, ex.Message))

                End Try

            Else

                Throw Me.GetLoggedNotAuthorizedEx(msg.Header)

            End If

        End Function


        <IWebMethod( _
           Company:="DotNetNuke", _
           Application:="IWeb", _
           Name:="GetPortalSpaceUsed", _
           Description:="Gets portal free space information.", _
           Group1:="IWeb FileManager", Group2:="Portal", _
           DefaultSecuritySetting:=IWebMethodAttribute.MethodSecurityLevels.Host)> _
        Public Function GetPortalSpaceUsed(ByVal msg As IWebRequest(Of Boolean)) As IWebResponse(Of String) Implements IWebFileManager.GetPortalSpaceUsed

            Dim objIWebAuthendication As New IWebAuthentication(msg.Header)
            If Not objIWebAuthendication.ValidAndAuthorized() Then
                Return New IWebResponse(Of String)("Not Authorized")
            End If

            Dim strUsed As String

            Dim portalCtrl As New PortalController


            Dim spaceUsed As Long = portalCtrl.GetPortalSpaceUsedBytes(msg.Header.PortalID)

            If spaceUsed < 1024 Then
                strUsed = spaceUsed.ToString("0.00") + "B"
            ElseIf spaceUsed < (1024 * 1024) Then
                strUsed = (spaceUsed / 1024).ToString("0.00") + "KB"
            Else
                strUsed = (ConvertToMB(spaceUsed)).ToString("0.00") + "MB"
            End If

            If msg.Parameter1 Then


                Dim hostingSpace As String

                Dim hostingSpaceMB As Integer = GetPortalInfo(msg.Header.PortalID).HostSpace

                If hostingSpaceMB > 0 Then

                    hostingSpace = String.Format("{0}MB", hostingSpaceMB)

                Else

                    hostingSpace = "[unlimited]"

                End If


                Return New IWebResponse(Of String)(String.Format(Pattern_TotalSpaceUsedWithQuota, strUsed, hostingSpace))


            Else

                Return New IWebResponse(Of String)(String.Format(Pattern_TotalSpaceUsed, strUsed))

            End If
        End Function


        <IWebMethod( _
           Company:="DotNetNuke", _
           Application:="IWeb", _
           Name:="IsFileTypeAllowed", _
           Description:="Returns true if file type is allowed in host settings.", _
           Group1:="IWeb FileManager", Group2:="Portal", _
           DefaultSecuritySetting:=IWebMethodAttribute.MethodSecurityLevels.Host)> _
        Public Function IsFileTypeAllowed(ByVal msg As IWebRequest(Of String)) As IWebResponse(Of Boolean) Implements IWebFileManager.IsFileTypeAllowed

            Dim objIWebAuthendication As New IWebAuthentication(msg.Header)
            If Not objIWebAuthendication.ValidAndAuthorized() Then

                Throw Me.GetLoggedNotAuthorizedEx(msg.Header)

            End If

            Return New IWebResponse(Of Boolean)(CheckFileType(msg.Parameter1))

        End Function

#End Region


    End Class


End Namespace